package com.hdsx.restful.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * 加密算法
 * @author kemi
 *
 */

public class Encipher {
	/**
	 * 加密
	 * 
	 * @param content
	 *            �?��加密的内�?
	 * @param password
	 *            加密密码
	 * @return
	 */
	public static String encryptAes(String content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码�?
			byte[] byteContent = content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始�?
			byte[] result = cipher.doFinal(byteContent);
			return parseByte2HexStr(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密
	 * 
	 * @param content
	 *            待解密内�?
	 * @param password
	 *            解密密钥
	 * @return
	 */
	public static String decryptAes(String content, String password) {
		try {

			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码�?
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始�?
			byte[] byteContent = parseHexStr2Byte(content);
			byte[] result = cipher.doFinal(byteContent);
			return new String(result, "utf-8"); // 加密
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 进行base64加密
	 * 
	 * @param str
	 *            待加密字符串
	 * @return
	 */
	public static String encryptBase64(String str) {
		if (str == null)
			return null;
		BASE64Encoder encoder = new BASE64Encoder();
		byte[] buf;
		try {
			buf = str.getBytes("utf-8");
			return encoder.encode(buf);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 对base64字符串进行解�?
	 * 
	 * @param str
	 * @return
	 */
	public static String decryptBase64(String str) {
		if (str == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] b;
		try {
			b = decoder.decodeBuffer(str);
			return new String(b);
		} catch (IOException e) {
			return null;
		}
	}
    /**
     * 进行MD5加密
     * @param password
     * @return
     */
	public static String encryptMD5(String password) {
		MessageDigest md;
		try {
			// 生成�?��MD5加密计算摘要
			md = MessageDigest.getInstance("MD5"); // 计算md5函数
			md.update(password.getBytes());
			String pwd=parseByte2HexStr(md.digest());
			return pwd;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return password;
	}

	/**
	 * 将二进制转换�?6进制
	 * 
	 * @param buf
	 * 
	 * @return
	 */

	public static String parseByte2HexStr(byte buf[]) {

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < buf.length; i++) {

			String hex = Integer.toHexString(buf[i] & 0xFF);

			if (hex.length() == 1) {

				hex = '0' + hex;

			}

			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * �?6进制转换为二进制
	 * 
	 * @param hexStr
	 * 
	 * @return
	 */

	public static byte[] parseHexStr2Byte(String hexStr) {

		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];

		for (int i = 0; i < hexStr.length() / 2; i++) {

			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);

			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),16);

			result[i] = (byte) (high * 16 + low);

		}
		return result;
	}
	public static void main(String[] args) {
		String content = "http://127.0.0.1:8080/easyui/page/longin.jsp";
		String password = "hdsx@68714962";
		String eAes = encryptAes(content, password);
		String eBase64=encryptBase64(eAes);
		String dBase64=decryptBase64(eBase64);
		String dAes = decryptAes(eAes, password);
		System.out.println("eAes:"+eAes + "\n;eBase64:"+eBase64+"\n;dBase64:"+dBase64+"\n;dAes:" + dAes);
		// System.out.println(encryptBase64(encryptAes(content, password)));
		// System.out.println(decryptBase64(decryptAes(content, password)));
		System.out.print(encryptMD5("228103"));
	}
}
